﻿#region 命名空间

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Gather
{
    [Access("272001,272003")]
    public partial class RuleNode : WebFactory.PageBase
    {
        private readonly GatherManage _myBll = new GatherManage();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlNode.DataSource = this._myBll.BindNode(int.Parse(Request.QueryString["PID"]));
                this.ddlNode.DataTextField = "ID";
                this.ddlNode.DataValueField = "ID";
                this.ddlNode.DataBind();

                if (this.ddlNode.Items.Count == 0)
                {
                    Action = "Add";
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                }
            }
        }

        private string Action
        {
            set { ViewState["Action"] = value; }
            get { return ViewState["Action"] == null ? string.Empty : ViewState["Action"].ToString(); }
        }

        private int Id
        {
            set { ViewState["ID"] = value; }
            get { return ViewState["ID"] == null ? 0 : Convert.ToInt32(ViewState["ID"]); }
        }

        private void Bind(int ID)
        {
            var model = this._myBll.GetInfo(ID);

            this.txtCategoryArea.Text = model.CategoryArea;
            this.txtCategoryPageNextUrl.Text = model.CategoryPageNextUrl;
            this.txtContentPageUrl.Text = model.ContentPageUrl;
            this.txtFullArticleUrl.Text = model.FullArticleUrl;
            this.txtIgnoreUrl.Text = model.IgnoreUrl;
            this.txtContentTitle.Text = model.ContentTitle;
            this.txtContent.Text = model.Content;
            this.txtContentPageNextUrl.Text = model.ContentPageNextUrl;
            this.txtFrom.Text = model.ComeFrom;
            this.txtAuthor.Text = model.Author;
            this.txtUpdateTime.Text = model.UpdateTime;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            var model = this._myBll.GetInfo(Id);
            if (model == null)
            {
                model = new WinnerCMS.Model.Gather();
                var pinfo = this._myBll.GetInfo(int.Parse(Request["PID"]));
                model.CategoryId = pinfo.CategoryId;
            }
            model.Name = string.Empty;
            model.StartUrl = string.Empty;
            model.CategoryArea = this.txtCategoryArea.Text;
            model.CategoryPageNextUrl = this.txtCategoryPageNextUrl.Text;
            model.ContentPageUrl = this.txtContentPageUrl.Text;
            model.ContentTitle = this.txtContentTitle.Text;
            model.Content = this.txtContent.Text;
            model.ContentPageNextUrl = this.txtContentPageNextUrl.Text;
            model.FullArticleUrl = this.txtFullArticleUrl.Text;
            model.IgnoreUrl = this.txtIgnoreUrl.Text;
            model.ComeFrom = this.txtFrom.Text;
            model.Author = this.txtAuthor.Text;
            model.UpdateTime = this.txtUpdateTime.Text;
            model.Filter = string.Empty;

            model.Replace = string.Empty;
            model.CanDrawWaterMark = false;
            model.IsExpert = true;
            model.PID = int.Parse(Request.QueryString["PID"]);


            var res = this._myBll.Save(model);
            if (string.IsNullOrEmpty(res.Errors))
            {
                MsgBox.Message("保存成功!",
                    "RuleNode.aspx?ModelId=" + Request["ModelId"] + "&PID=" + Request.QueryString["PID"],
                    this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }


        protected void btnGoToEdit_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            Action = "Edit";
            Id = int.Parse(this.ddlNode.SelectedValue);
            Bind(Id);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            Action = "Add";
            Id = 0;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this._myBll.Delete(this.ddlNode.SelectedValue);
            MsgBox.Message("删除成功!", "RuleNode.aspx?ModelId=" + Request["ModelId"] + "&PID=" + Request.QueryString["PID"],
                this.UpdatePanel1);
        }
    }
}